/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * WorkTaskController
 * 日程任务
 * 修改纪录
 * 2020-11-02 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2020-11-02
 */
package com.topscomm.work.controller;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.ParameterCache;
import com.topscomm.work.controller.auto.WorkTaskControllerAuto;
import com.topscomm.work.pojo.WorkTaskEntity;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUnit;

/**
 * @author: zhanglongfei
 * @date: 2020-11-02
 * @description:日程任务Controller
 * @modify:
 */
@RestController
@RequestMapping("work/task")
public class WorkTaskController extends WorkTaskControllerAuto implements IWorkTaskController {
	/**
	 * @author: zhanglongfei
	 * @date: 2020-11-02
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: zhanglongfei
	 * @date: 2020-11-02
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description:查询周期中的日期列表
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020年11月5日下午4:12:01
	 * @modify:
	 */
	@GetMapping(value = "/queryPeriodDateList")
	public ResponseResult<Object> queryPeriodDateList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			int maxCount = ParameterCache.getIntValue("PeroidMaxCount", 100);
			if (ConvertUtil.convertToLong(paraMap.get("repeatcount")) > maxCount) {
				return handleControllerMessage("次数最大" + maxCount + "次");
			}
			WorkTaskEntity taskEntity = new WorkTaskEntity();
			taskEntity.convertFromMap(paraMap);
			List<Map<String, Object>> timeList = service.queryPeriodDateList(taskEntity);
			for (Map<String, Object> timeMap : timeList) {
				DateBetween duration = DateBetween.create(taskEntity.getBegintime(), taskEntity.getEndtime());
				long times = ConvertUtil.convertToInt(duration.between(DateUnit.MS));
				String beginString = DateUtil.dateToString(taskEntity.getBegintime(), "yyyy-MM-dd HH:mm:ss");
				Date beginTime = ConvertUtil
						.convertToDate(timeMap.get("date").toString() + " " + beginString.split(" ")[1]);
				Date endTime = new Date(Instant.ofEpochMilli(beginTime.getTime()).plusMillis(times).toEpochMilli());
				timeMap.put("date", DateUtil.dateToString(beginTime, "yy-MM-dd HH:mm") + "~"
						+ DateUtil.dateToString(endTime, "yy-MM-dd HH:mm"));
			}
			return ResponseResult.ok(timeList);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}
}
